<template>
  <div>
    <el-form inline :model="searchForm">
      <el-form-item>
        <el-input v-model="searchForm.name" clearable placeholder="分类名字"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList">查询</el-button>
        <el-button type="primary" @click="showDialog()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border>
      <el-table-column prop="typeId" label="编号" align="center" header-align="center"/>
      <el-table-column prop="name" label="分类名" align="center" header-align="center"/>
      <el-table-column prop="status" label="状态" align="center" header-align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="danger" size="small">禁用</el-tag>
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" header-align="center" fixed="right">
        <template slot-scope="scope">
          <el-button size="small" @click="showDialog(scope.row.typeId)">修改</el-button>
          <el-button size="small"  @click="deleteInfo(scope.row.typeId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="pageIndex" :page-size="pageSize" :total="totalPage" :page-sizes="[5,10,20,30,50,100,200]" layout="total,sizes,prev,pager,next,jumper" @current-change="currentChange" @size-change="sizeChange"></el-pagination>
    <TypeAddOrUpdate ref="addOrUpdate" @onCloseWin="getDataList"/>
  </div>
</template>

<script>
import TypeAddOrUpdate from '@/views/Type/TypeAddOrUpdate'
export default {
  name: 'ProSpec',
  components: {
    TypeAddOrUpdate
  },
  data () {
    return {
      searchForm: {
        name: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 5,
      totalPage: 0,
      dataListLoading: false,
      showAvatar: false,
      avatarSrc: ''
    }
  },
  methods: {
    getDataList () {
      this.dataListLoading = this.$loading({ text: '加载表格...' })
      this.$http.get('/type/list', {
        page: this.pageIndex,
        limit: this.pageSize,
        ...this.searchForm
      }).then(data => {
        this.dataListLoading.close()
        if (data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        }
      })
    },
    currentChange (val) {
      this.pageIndex = val
      this.getDataList()
    },
    sizeChange (val) {
      this.pageSize = val
      this.getDataList()
    },
    showDialog (typeId) {
      this.$refs.addOrUpdate.init(typeId)
    },
    deleteInfo (typeId) {
      this.$confirm(`确定删除编号${typeId}的数据吗？`).then(() => {
        this.dataListLoading = this.$loading({ text: '加载表格...' })
        this.$http.delete(`/type/delete/${typeId}`).then(data => {
          this.dataListLoading.close()
          if (data.code === 0) {
            this.$message({
              message: '删除成功！',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$alert(data.msg, { type: 'error' }).then(() => {
              this.getDataList()
            })
          }
        })
      })
    }
  },
  created () {
    this.getDataList()
  }
}
</script>

<style scoped>

</style>
